package com.wifi.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.bean.MacBean;
import com.wifi.eshipewifi.R;
import com.wifi.eshipewifi.SocThread;


public class ConnetAdapter extends BaseAdapter{

    private Context context;
    String[] data;
    BaseApplication application;
    int select_position=-1;
    String ssid;
    boolean isIng=false;
    public ConnetAdapter(Context context){
        this.context=context;
    }

    public ConnetAdapter(Context context, String[] data, BaseApplication application){
        this.context=context;
        this.data=data;
        this.application=application;
        //$starnet2;starnet2,24/70,true;starnet3,27/70,false\r\n ----wifi列表数据
    }

    public void setCheck(String ssid){
        this.ssid=ssid;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount(){
        return (data.length-1);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView = View.inflate(context, R.layout.item_connet, null);
            holder=new ViewHolder();
            holder.tv_item_name = (TextView) convertView.findViewById(R.id.tv_item_name);
            holder.tv_item_time = (TextView) convertView.findViewById(R.id.tv_item_time);
            holder.rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            holder.tv_item_black = (TextView) convertView.findViewById(R.id.tv_item_black);
            holder.iv_item_icon = (ImageView) convertView.findViewById(R.id.iv_item_icon);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //$starnet2;starnet2,24/70,true;starnet3,27/70,false\r\n ----wifi列表数据
        //$scan_wifi;mac地址,starnet2,24,true;mac地址,starnet3,27,false;----wifi列表数据
        final String[] item_data = data[position + 1].split(",");
//        holder.tv_item_name.setText(item_data[1]);//mac
        holder.tv_item_name.setText(item_data[0]);
//        final String isEncrypt=item_data[3];//mac
        final String isEncrypt=item_data[2];
//        holder.tv_item_time.setText("是否加密："+item_data[3]);//mac
        holder.tv_item_time.setText("是否加密："+item_data[2]);
//        Integer int_4g = Integer.valueOf(item_data[2]);//mac
        Integer int_4g = Integer.valueOf(item_data[1]);
        if(int_4g>-1 && int_4g<20){
            holder.iv_item_icon.setBackgroundResource(R.drawable.wifi_no);
        }else if(int_4g>19 && int_4g<40){
            holder.iv_item_icon.setBackgroundResource(R.drawable.wifi_1);
        }else if(int_4g>39 && int_4g<60){
            holder.iv_item_icon.setBackgroundResource(R.drawable.wifi_2);
        }else if(int_4g>59 && int_4g<80){
            holder.iv_item_icon.setBackgroundResource(R.drawable.wifi_3);
        }else if(int_4g>79 && int_4g<101){
            holder.iv_item_icon.setBackgroundResource(R.drawable.wifi_4);
        }

//        if(ssid!=null && ssid.indexOf(item_data[1])!=-1){//mac
        if(ssid!=null && ssid.indexOf(item_data[0])!=-1){
            //正在连接状态
            holder.tv_item_black.setText("已连接");

            holder.rl_item.setClickable(false);
            holder.iv_item_icon.setBackgroundResource(R.drawable.check);
        }else {
            holder.tv_item_black.setText("连接");
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(isEncrypt.indexOf("true")!=-1){//加密，跳密码框
                        final View view = View.inflate(context, R.layout.item_dialog, null);
                        AlertDialog dialog= new AlertDialog.Builder(context)
                                .setTitle("请输入密码")
                                .setIcon(android.R.drawable.ic_dialog_info)
                                .setView(view)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        EditText et = (EditText) view.findViewById(R.id.et_item);
                                        String password = et.getText().toString().trim();
                                        //todo mac地址加上
//                                        application.sendMsg("$connect_wifi;"+item_data[0]+","+item_data[1]+","+password+";");//mac
                                        application.sendMsg("$connect_wifi;"+item_data[0]+","+password+";");
                                        select_position=position;
//                                        ssid=item_data[1];//mac
                                        ssid=item_data[0];
                                        dialog.cancel();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                }).create();
                        dialog .show();
                    }else {//无密码，发送连接
                        Toast.makeText(context,"连接"+item_data[0],Toast.LENGTH_LONG).show();
//                        application.sendMsg("$connect_wifi;"+item_data[0]+","+item_data[1]+";");//mac
                        application.sendMsg("$connect_wifi;"+item_data[0]+";");
                    }
                }
            });

        }
        return convertView;
    }


    private class ViewHolder{

        private TextView tv_item_name;
        private TextView tv_item_time;
        private RelativeLayout rl_item;
        private TextView tv_item_black;
        private ImageView iv_item_icon;
    }
}
